package com.winit.openapi.tms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.tms.spi.transportationdoc.LadingBillService;
import com.winit.tms.spi.transportationdoc.TransportationDocService;
import com.winit.tms.spi.transportationdoc.command.LadingBillAddOrderCommand;
import com.winit.tms.spi.transportationdoc.command.LadingBillCommand;
import com.winit.tms.spi.transportationdoc.command.LadingBillDeleteCommand;
import com.winit.tms.spi.transportationdoc.command.LadingBillDeleteOrderCommand;
import com.winit.tms.spi.transportationdoc.command.LadingBillOrderCombineCreateCommand;
import com.winit.tms.spi.transportationdoc.command.LadingBillOrderCombineQueryCommand;
import com.winit.tms.spi.transportationdoc.vo.LadingBillDeclarenameVo;
import com.winit.tms.spi.transportationdoc.vo.LadingBillOrderCombineVo;
import com.winit.tms.spi.transportationdoc.vo.LadingBillVo;
import com.winit.tms.spi.transportationdoc.vo.TransportationDocOrderVo;

/**
 * 预约单关联单据操作 Controller
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * ming.chen 	1.0  		2017-9-8 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/tms/transportDoc/ladingBill")
public class LadingBillController extends BaseController {

	@Resource
	private LadingBillService ladingBillService;
	@Resource
	private TransportationDocService transportationDocService;

    /**
     * 按combineId查询已绑订单列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "getCombineOrders")
    public String getCombineOrders(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillOrderCombineQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillOrderCombineQueryCommand();
            command.setCtx(CommandContext.getContext());
            command.setCombineId(json.getLong("combineId"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        List<TransportationDocOrderVo> orderList = ladingBillService.getCombineOrdersByCombineId(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        data.put("orderList", orderList);
        data.put("combineId", command.getCombineId());
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    /**
     * 按winitOrderNo查询提单绑定信息
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "getCombineInfo")
    public String getCombineInfo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillOrderCombineQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillOrderCombineQueryCommand();
            command.setCtx(CommandContext.getContext());
            if(json.containsKey("combineId")){
            	command.setCombineId(json.getLong("combineId"));
            }
            command.setOrderNo(json.getString("orderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        LadingBillOrderCombineVo combineVo = null;
        if(null != command.getCombineId()){
        	 combineVo = ladingBillService.getLadingBillCombineInfoByCombineId(command);
        }else{
        	 combineVo = ladingBillService.getLadingBillCombineInfoByOrderNo(command);
        }
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        data.put("orderList", combineVo.getOrders());
        data.put("ladingBillList", combineVo.getLadingBills());
        data.put("combineId", command.getCombineId());
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 分页查询可添加至提单绑定信息的订单列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "getUnCombineOrders")
    public String getUnCombineOrders(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillOrderCombineQueryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillOrderCombineQueryCommand();
            command.setCtx(CommandContext.getContext());
            command.setCombineId(json.getLong("combineId"));
            command.setCustomerCode(this.getCurrentCustomerCode());
            Searchable searchable = super.buildSearchablePageParams(json);
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Page<TransportationDocOrderVo>  page = ladingBillService.queryOrdersForLadingBillCombineByPage(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        data.put("orderList", page.getContent());
        data.put("combineId", command.getCombineId());
        Map<String, Object> pageMap = getPageParams(page);
        data.put("pageParams", pageMap.get("pageParams"));
        responseMsg.setData(data);
        return SUCCESS;
    }

    
    /**
     * 创建提单绑定信息
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "createCombineInfo")
    public String createCombineInfo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillOrderCombineCreateCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillOrderCombineCreateCommand();
            List<String> orderNos = new ArrayList<String>();
            if (json.containsKey("orderNos")) {
                JSONArray orderNoList = json.getJSONArray("orderNos");
                for (Object object : orderNoList) {
                	String orderNo = (String) object;
                	orderNos.add(orderNo);
                }
            }
            command.setOrderNos(orderNos);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Long combineId = ladingBillService.createLadingBillCombineInfo(command);
        LadingBillOrderCombineVo combineVo = null;
        if(null != combineId){
        	LadingBillOrderCombineQueryCommand queryCommand = new LadingBillOrderCombineQueryCommand();
        	queryCommand.setCombineId(combineId);
        	combineVo = ladingBillService.getLadingBillCombineInfoByCombineId(queryCommand);
        }
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        if(null != combineVo){
        	data.put("orderList", combineVo.getOrders());
        	data.put("ladingBillList", combineVo.getLadingBills());
        	data.put("combineId", combineId);
        }
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    /**
     * 更新提单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "updateLadingBill")
    public String updateLadingBill(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillCommand();
            LadingBillVo ladingBill  = new LadingBillVo();
            ladingBill.setCombineId(json.getLong("combineId"));
            if (json.containsKey("ladingBill")) {
            	JSONObject ladingBillObject = json.getJSONObject("ladingBill");
            	if(ladingBillObject.containsKey("consigneeId")){
            		ladingBill.setConsigneeId(ladingBillObject.getLong("consigneeId"));
            	}
            	if(ladingBillObject.containsKey("shipperId")){
            		ladingBill.setShipperId(ladingBillObject.getLong("shipperId"));
            	}
            	if(ladingBillObject.containsKey("packageQty")){
            		ladingBill.setPackageQty(ladingBillObject.getInteger("packageQty"));
            	}
            	if(ladingBillObject.containsKey("chargeWeight")){
            		ladingBill.setChargeWeight(ladingBillObject.getBigDecimal("chargeWeight"));
            	}
            	if(ladingBillObject.containsKey("volume")){
            		ladingBill.setVolume(ladingBillObject.getBigDecimal("volume"));
            	}
            	if(ladingBillObject.containsKey("weight")){
            		ladingBill.setWeight(ladingBillObject.getBigDecimal("weight"));
            	}
            	if(ladingBillObject.containsKey("ladingBillId")){
            		ladingBill.setId(ladingBillObject.getLong("ladingBillId"));
            	}
            	if(ladingBillObject.containsKey("declareNameList")){
            		 JSONArray declareNameArray = ladingBillObject.getJSONArray("declareNameList");
            		 List<LadingBillDeclarenameVo> declareNameList = new ArrayList<LadingBillDeclarenameVo>();
                     for (Object declareNameListObject : declareNameArray) {
                     	String declareName = (String) declareNameListObject;
                     	LadingBillDeclarenameVo declareNameVo = new LadingBillDeclarenameVo();
                     	declareNameVo.setDeclareName(declareName);
                     	declareNameVo.setLadingBillId(ladingBill.getId());
                     	declareNameList.add(declareNameVo);
                     }
            		ladingBill.setDeclareNameList(declareNameList);
            	}
            }
            command.setLadingBillVo(ladingBill);
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ladingBillService.updateLadingBill(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        //data.put("orderList", combineVo.getOrders());
        //data.put("ladingBillList ", combineVo.getLadingBills());
        //data.put("combineId", command.getCombineId());
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    /**
     * 创建提单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "addLadingBill")
    public String addLadingBill(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillCommand();
            LadingBillVo ladingBill  = new LadingBillVo();
            ladingBill.setCombineId(json.getLong("combineId"));
            if (json.containsKey("ladingBill")) {
            	JSONObject ladingBillObject = json.getJSONObject("ladingBill");
            	if(ladingBillObject.containsKey("consigneeId")){
            		ladingBill.setConsigneeId(ladingBillObject.getLong("consigneeId"));
            	}
            	if(ladingBillObject.containsKey("shipperId")){
            		ladingBill.setShipperId(ladingBillObject.getLong("shipperId"));
            	}
            	if(ladingBillObject.containsKey("packageQty")){
            		ladingBill.setPackageQty(ladingBillObject.getInteger("packageQty"));
            	}
            	if(ladingBillObject.containsKey("chargeWeight")){
            		ladingBill.setChargeWeight(ladingBillObject.getBigDecimal("chargeWeight"));
            	}
            	if(ladingBillObject.containsKey("volume")){
            		ladingBill.setVolume(ladingBillObject.getBigDecimal("volume"));
            	}
            	if(ladingBillObject.containsKey("weight")){
            		ladingBill.setWeight(ladingBillObject.getBigDecimal("weight"));
            	}
            	if(ladingBillObject.containsKey("declareNameList") || null != ladingBillObject.get("declareNameList")){
            		 JSONArray declareNameArray = ladingBillObject.getJSONArray("declareNameList");
            		 List<LadingBillDeclarenameVo> declareNameList = new ArrayList<LadingBillDeclarenameVo>();
            		 if(!CollectionUtils.isEmpty(declareNameList)){
            			 for (Object declareNameListObject : declareNameArray) {
            				 String declareName = (String) declareNameListObject;
            				 LadingBillDeclarenameVo declareNameVo = new LadingBillDeclarenameVo();
            				 declareNameVo.setDeclareName(declareName);
            				 declareNameVo.setLadingBillId(ladingBill.getId());
            				 declareNameList.add(declareNameVo);
            			 }
            			 ladingBill.setDeclareNameList(declareNameList);
            		 }
            	}
            }
            command.setLadingBillVo(ladingBill);
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Long ladingBillId = ladingBillService.addLadingBill(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        data.put("ladingBillId", ladingBillId);
        //data.put("orderList", combineVo.getOrders());
        //data.put("ladingBillList ", combineVo.getLadingBills());
        //data.put("combineId", command.getCombineId());
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    /**
     * 增加订单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "addOrder")
    public String addOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillAddOrderCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillAddOrderCommand();
            List<String> orderNos = new ArrayList<String>();
            if (json.containsKey("orderNos")) {
                JSONArray orderNoList = json.getJSONArray("orderNos");
                for (Object object : orderNoList) {
                	String orderNo = (String) object;
                	orderNos.add(orderNo);
                }
            }
            command.setOrderNoList(orderNos);
            command.setCombineId(json.getLong("combineId"));
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ladingBillService.addOrder(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        //data.put("orderList", combineVo.getOrders());
        //data.put("ladingBillList ", combineVo.getLadingBills());
        //data.put("combineId", command.getCombineId());
        return SUCCESS;
    }
    
    
    /**
     * 删除订单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "removeOrder")
    public String removeOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillDeleteOrderCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillDeleteOrderCommand();
            List<String> orderNos = new ArrayList<String>();
            if (json.containsKey("orderNos")) {
                JSONArray orderNoList = json.getJSONArray("orderNos");
                for (Object object : orderNoList) {
                	String orderNo = (String) object;
                	orderNos.add(orderNo);
                }
            }
            command.setOrderNos(orderNos);
            command.setCombineId(json.getLong("combineId"));
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ladingBillService.removeOrder(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        //data.put("orderList", combineVo.getOrders());
        //data.put("ladingBillList ", combineVo.getLadingBills());
        //data.put("combineId", command.getCombineId());
        return SUCCESS;
    }
    
    
    /**
     * 删除提单
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "deleteLadingBill")
    public String deleteLadingBill(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LadingBillDeleteCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = new LadingBillDeleteCommand();
            if (json.containsKey("ladingBillId")) {
            	command.setId(json.getLong("ladingBillId"));
            }
            command.setCombineId(json.getLong("combineId"));
            command.setCtx(CommandContext.getContext());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ladingBillService.removeLadingBill(command);
        Map<String, Object> data = new HashMap<String, Object>();
        responseMsg.setData(data);
        //data.put("orderList", combineVo.getOrders());
        //data.put("ladingBillList ", combineVo.getLadingBills());
        //data.put("combineId", command.getCombineId());
        return SUCCESS;
    }

   
}
